package com.bwt.usermanage;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.bwt.usermanage.Entity.User;
import com.bwt.usermanage.Mapper.UserDao;
import com.bwt.usermanage.adapter.UserAdapter;
import com.bwt.usermanage.utils.DialogUtil;
import com.bwt.usermanage.utils.MoreDialog;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnEdit;
    private ListView main_lv;
    private ImageView main_iv_search;
    private ImageButton main_btn_more;
    private UserDao userDao;
    private List<User> mDatas;
    UserAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userDao= AppDatabase.getInstance(this).userDao();

        btnEdit = findViewById(R.id.main_btn_edit);
        main_lv = findViewById(R.id.main_lv);
        main_btn_more = findViewById(R.id.main_btn_more);
        main_iv_search = findViewById(R.id.main_iv_search);
        mDatas = new ArrayList<>();
        //设置适配器：加载每一行数据到列表当中
        adapter = new UserAdapter(this, mDatas);
        main_lv.setAdapter(adapter);
        setLVLongClickListener();
        setLVOnItemClickListener();

        btnEdit.setOnClickListener(this);
        main_iv_search.setOnClickListener(this);
        main_btn_more.setOnClickListener(this);


    }

    private void setLVOnItemClickListener() {

        main_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                User clickBean = mDatas.get(position);//获取正在被点击的这条信息
                Bundle bundle = new Bundle();
                bundle.putInt("uid", clickBean.uid);

                Intent intent = new Intent();
                intent.putExtras(bundle);
                intent.setClass(MainActivity.this, InsertOrEditActivity.class);
                startActivity(intent);
            }
        });

    }

    /** 设置ListView的长按事件*/
    private void setLVLongClickListener() {
        main_lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                User clickBean = mDatas.get(position);//获取正在被点击的这条信息

                //弹出提示用户是否删除的对话框
                showDeleteItemDialog(clickBean);
                return false;
            }
        });
    }

    private void showDeleteItemDialog(final User clickBean) {
        DialogUtil.showMsgDialog(this, "提示信息", "您确定要删除这条记录么？", null, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //执行删除的操作
                userDao.delete(clickBean);
                mDatas.remove(clickBean);   //实时刷新，移除集合当中的对象
                adapter.notifyDataSetChanged();   //提示适配器更新数据
            }
        });

    }

    // 当activity获取焦点时，会调用的方法
    @Override
    protected void onResume() {
        super.onResume();
        loadDBData();
    }

    // 加载数据库数据
    private void loadDBData() {

        List<User> list = userDao.l1oadAll();
        mDatas.clear();
        mDatas.addAll(list);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_btn_edit:
                Intent it = new Intent(this, InsertOrEditActivity.class);  //跳转界面
                startActivity(it);
                break;

            case R.id.main_iv_search:
                Intent it1 = new Intent(this, SearchActivity.class);  //跳转界面
                startActivity(it1);
                break;

            case R.id.main_btn_more:
                MoreDialog moreDialog = new MoreDialog(this);
                moreDialog.show();
                moreDialog.setDialogSize();
                break;
        }
    }
}